package com.zmzncs.lmtc.module.promotion.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zmzncs.lmtc.module.promotion.pojo.ticket.Ticket;
import com.zmzncs.lmtc.module.promotion.dao.TicketDao;
import com.zmzncs.lmtc.module.promotion.pojo.ticket.TicketAddForm;
import com.zmzncs.lmtc.module.promotion.pojo.ticket.TicketDTO;
import com.zmzncs.lmtc.module.promotion.pojo.ticket.TicketSelectForm;
import com.zmzncs.lmtc.module.promotion.service.TicketService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 任建波
 * @since 2020-10-13
 */
@Service
public class TicketServiceImpl extends ServiceImpl<TicketDao, Ticket> implements TicketService {

    @Autowired
    private TicketDao ticketDao;

    @Override
    public void addTicket(TicketAddForm form) {
        Ticket ticket = new Ticket();
        ticket.setTicketName(form.getTicketName());
        ticket.setUseLimitMoney(form.getUseLimitMoney());
        ticket.setPreferentialMoney(form.getPreferentialMoney());
        ticket.setPreferentialType(form.getPreferentialType());
        ticket.setUseType(form.getUseType());
        ticket.setExpiryDays(form.getExpiryDays());
        ticket.setStatus(0);
        ticket.setCreateTime(new Date());
        ticketDao.insert(ticket);
    }

    @Override
    public IPage<TicketDTO> getTicketByManage(TicketSelectForm form) {
        Page<Object> page = new Page<>(form.getCurrent(), form.getSize());
        //条件分页查询优惠券信息
        IPage<TicketDTO> iPage = ticketDao.getTicketByManage(page,form);
        List<TicketDTO> records = iPage.getRecords();
        if (records != null && !records.isEmpty()){
            List<TicketDTO> ticketDTOS = preferentialInfo(records);
            iPage.setRecords(ticketDTOS);
        }
        return iPage;
    }

    @Override
    public void updateTicketStatus(Long ticketId, Integer status) {
        Ticket ticket = new Ticket();
        ticket.setId(ticketId);
        ticket.setStatus(status);
        ticketDao.updateById(ticket);
    }

    @Override
    public List<TicketDTO> getAllTicket() {
        List<TicketDTO> ticketDTOS = ticketDao.getAllTicket();
        if (ticketDTOS != null && !ticketDTOS.isEmpty()){
            ticketDTOS = preferentialInfo(ticketDTOS);
        }
        return ticketDTOS;
    }

    /**
     * 处理优惠券内容信息
     */
    private List<TicketDTO> preferentialInfo(List<TicketDTO> list){
        for (TicketDTO record : list) {
            Integer preferentialType = record.getPreferentialType();
            //使用界限金额
            BigDecimal useLimitMoney = record.getUseLimitMoney();
            //优惠内容
            BigDecimal preferentialMoney = record.getPreferentialMoney();

            //金额限制
            if (useLimitMoney.compareTo(BigDecimal.ZERO) == 1){
                //满减
                if (preferentialType == 1){
                    record.setPreferentialInfo("满" + useLimitMoney.doubleValue() + "元减" + preferentialMoney.doubleValue() + "元");
                    //折扣
                } else {
//                    preferentialMoney = preferentialMoney.multiply(new BigDecimal(10));
                    record.setPreferentialInfo("满" + useLimitMoney.doubleValue() + "元," + "享" + preferentialMoney.doubleValue() + "折优惠");
                }
                //无门槛
            } else {
                //满减
                if (preferentialType == 1){
                    record.setPreferentialInfo("无门槛，" + "减" + preferentialMoney.doubleValue() + "元");
                    //折扣
                } else {
//                    preferentialMoney = preferentialMoney.multiply(new BigDecimal(10));
                    record.setPreferentialInfo("无门槛，" + "享" + preferentialMoney.doubleValue() + "折优惠");
                }
            }
        }
        return list;
    }


}
